package cate.game.activity.treasure.data;

import cate.common.table.activity.treasure.row.TreasureExchangeRow;
import cate.common.util.GameResult;
import cate.game.GameBody;
import cate.game.activity.treasure.TreasureActivity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;

public class TreasureCallExchangeItem {
    @NoteField(value = "购买项配置ID")
    public int itemTid;
    @NoteField(value = "购买数量")
    public int buyNum;

    public TreasureCallExchangeItem() {
    }

    public TreasureCallExchangeItem(int itemTid) {
        this.itemTid = itemTid;
    }

    /**
     * 重置购买计数
     */
    public void resetBuyNum() {
        buyNum = 0;
    }

    /**
     * 增加购买数量
     *
     * @param num 增量
     */
    public void addBuyNum(int num) {
        buyNum += num;
    }

    /**
     * 获取配置
     */
    @JsonIgnore
    public TreasureExchangeRow getConfigRow(GameBody game) {
        return game.table.treasure.exchange.get(itemTid);
    }

    /**
     * 检测是否允许购买
     *
     * @param num 购买数量
     * @return 检测结果 无携带数据
     */
    public GameResult<TreasureActivity> checkBuyAllow(GameBody game, int num) {
        GameResult<TreasureActivity> r = new GameResult<>();
        TreasureExchangeRow configRow = game.table.treasure.exchange.get(itemTid);
        if (configRow == null) {
            return r.fail("找不到配置");
        }
        if (configRow.limitTimes > 0 && buyNum > configRow.limitTimes - num) {
            return r.fail("限购数量不足");
        }
        return r.success();
    }
}
